import os
import re

import pandas as pd
import qtmodern.styles
import qtmodern.windows
from PySide6.QtCore import Signal, QThread, QSettings
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from common import index
from loading import LoadingAnimation
from ui.aa_ui import Ui_Form


class LongTaskThread(QThread):
    finished = Signal()

    def __init__(self,zd):
        super().__init__()
        self.zd=zd

    def run(self):
        try:
            from pyfile import getdata
            test=getdata.Test()
            test.getData3(self.zd)
        except Exception as e:
            index.show_message(e, 0)
        finally:
            self.finished.emit()

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        settings = QSettings('MyCompany', 'MyApp')
        self.ui.txt1.setText(settings.value('t1', ''))
        self.ui.txt2.setText(settings.value('t2', ''))
        self.ui.txt3.setText(settings.value('t3', ''))
        self.ui.txt4.setText(settings.value('t4', ''))
        self.bind()

    def bind(self):

        self.ui.but1.clicked.connect(self.click1)
        self.ui.but2.clicked.connect(self.click2)
        self.ui.but3.clicked.connect(self.click3)
        self.ui.pushButton.clicked.connect(self.click)

    def click1(self):

        path=index.open_file_dialog()
        self.ui.txt1.setText(path)
        settings = QSettings('MyCompany', 'MyApp')
        settings.setValue('t1', path)
    def click2(self):
        path=index.open_file_dialog()
        self.ui.txt2.setText(path)
        settings = QSettings('MyCompany', 'MyApp')
        settings.setValue('t2', path)
    def click3(self):
        path=index.open_excel_dialog()
        self.ui.txt3.setText(path)
        settings = QSettings('MyCompany', 'MyApp')
        settings.setValue('t3', path)

    def click(self):
        settings = QSettings('MyCompany', 'MyApp')
        settings.setValue('t4', self.ui.txt4.text())
        # 使用 split 方法拆分字符串，支持英文逗号和中文逗号
        result_list = [item.strip() for item in re.split(r',|，', self.ui.txt4.text()) if item]
        self.zd = {'按期交货率': self.ui.txt1.text(), '月份一次交检合格率': self.ui.txt2.text(),
                   '目标文件': self.ui.txt3.text(), '所选月份': result_list}
        self.ui.longTaskThread = LongTaskThread(self.zd)
        self.ui.finished = self.ui.longTaskThread.finished.connect(self.on_finished)
        current_dir = os.path.dirname(os.path.abspath(__file__))  # 获取当前文件所在目录路径
        self.ui.LoadingAnimation = LoadingAnimation(os.path.join(current_dir, 'gif', '33.gif'))
        self.ui.pushButton.setEnabled(False)
        self.ui.LoadingAnimation.show()
        self.ui.longTaskThread.start()

    def on_finished(self):
        self.ui.pushButton.setEnabled(True)
        self.ui.LoadingAnimation.hide()

    def get2003(self):
        df = pd.read_excel(self.ui.txt3.text(), sheet_name='Sheet1', usecols=['序号', '供应商名称', '采购类别'])
        return df


if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    qtmodern.styles.dark(app)
    mw = qtmodern.windows.ModernWindow(window)
    mw.move(app.primaryScreen().size().width() / 2 - window.size().width() / 2,
            app.primaryScreen().size().height() / 2 - window.size().height() / 2)
    mw.show()
    app.exec()
